//index.js
//获取应用实例
const app = getApp();
var router = require("../../router.js");
Page({
    data: {
      login: 0,
      content: 1, //内容
      tabBar: 1,//底部
      logo: false,     //登录框
      safe: [ //保险套餐数据          
      ],
      active_list: [ //户外活动数据 收费                       
      ],
      free_active_list: [ //户外活动数据 免费            
      ],
      imgUrls: [ //banner图片数据          
      ],
      juvenile: [          
      ],
      indicatorDots: true, //是否添加轮播小圆点
      autoplay: true, //是否自动播放
      interval: 5000, //轮播间隔时间
      duration: 1000, //动画过渡时间
      user : 3,
      userPid: 0
    },

    go_page_buy(e) {  //跳转购买保险页      
        let id = e.currentTarget.dataset.id;      
        app.navTo('../buy/buy?id=' + id)
    },
    go_page(e){
      var path = e.currentTarget.dataset.path;
      wx.navigateTo({
        url: path,
      })
    },
    to_detail(e) {  //跳转活动详情页   
        var info_type = e.currentTarget.dataset.type;
        var index = e.currentTarget.dataset.index;
        var list = '';
        if(info_type == 1){
          list = this.data.active_list;
        } else if (info_type == 2){
          list = this.data.free_active_list;
        }else{
          list = this.data.juvenile;
        }
      app.navTo('../active_detail/active_detail?id=' + list[index].id)            
    },
    go_buy_safe(event){
      var index = event.currentTarget.dataset.id;   
      app.navTo('../buy/buy?id=' + this.data.safe[index]['id'])       
    },
    onLoad: function (options) {
      var scene = decodeURIComponent(options.scene)    
      if (!isNaN(scene)) {       
        this.setData({
          userPid: scene
        })
      }    
      wx.hideTabBar({});           
    },
    onShow: function(){    
      this.isLogin();
      var that = this;
      app.request({
        url: router.index.audit,
        method: "GET",
        success: function (data) {
          that.setData({
            login: data.login,
            content: data.content,
            tabBar: data.tabbar,
          })
        }
      });
      this.getInfo();
      wx.showShareMenu({
        withShareTicket: true
      })
    },
  // 底部
  index: function () {
    app.radTo('../index/index')
  },
  underLine: function () {
    app.radTo('../underLine/underLine')
  },
  course: function () {
    app.radTo('../course/course')
  },
  distribution: function () {
    app.radTo('../distribution/distribution')
  },
  mine: function () {
    app.radTo('../mine/mine')
  },
    onShareAppMessage: function (res) {
      var path = '/pages/index/index';
      if (wx.getStorageSync('userInfo')) {
        var id = wx.getStorageSync("userInfo")['id'];
        path += '?scene=' + id;
      }
      return {
        title: "诺创云拓",
        path: path,
      }      
    },
    sendRequest: function (e) {    
      var that = this;
      wx.showLoading({
        title: '请稍后',
        success: function () {
          wx.login({
            success: function (result) {    
              console.log(result)
              wx.request({
                url: router.user.login,
                method: "POST",
                data: {
                  code: result.code,
                  encryptedData: e.detail.encryptedData,
                  iv: e.detail.iv,
                  userPid: that.data.userPid
                },
                success: function (res) {  
                  console.log('uinddd',res)                  
                  if (res.data.status === true) {
                    wx.setStorageSync("userInfo", res.data.user);
                    wx.setStorageSync("grade", res.data.grade);//此地整个方法用不到，因为index_new还有一模一样的
                    that.setData({                   
                      logo: false
                    })
                    wx.hideLoading();
                    wx.showToast({
                      title: '恭喜你登陆成功',
                      duration: 2000,
                      success: function () {
                        wx.showTabBar({});
                        that.getInfo(); 
                      }
                    })
                  } else {
                    wx.hideLoading();
                    wx.showToast({
                      title: '登录失败,请重新点击授权',
                      content: '请重新点击授权',
                      icon: 'none'
                    })
                  }
                },
                fail: function () {
                  wx.hideLoading();
                  wx.showToast({
                    title: '网络错误',
                    content: '请重新点击授权',
                    icon: 'none'
                  })
                }
              })
            }          
          });
        }
      });
    },
    isLogin: function () {
      var userInfo = wx.getStorageSync("userInfo");         
      if (userInfo) {
        wx.showTabBar({}) 
        this.setData({         
          logo: false
        })
      } else {       
        this.setData({       
          logo: true
        })
      }
    },
    getInfo: function () {
      if (wx.getStorageSync('userInfo')){
        var that = this;
        app.request({
          url: router.index.index,
          method: "POST",
          data: {
            userId: wx.getStorageSync('userInfo').id,
          },
          success: function (data) {
            if (data.status == true) {
              that.setData({
                user: data.user.type,
                safe: data.safe,
                active_list: data.expand,
                free_active_list: data.free_expand,
                juvenile: data.juvenile,
                imgUrls: data.banner,
              });
            } else {
              wx.showToast({
                title: '获取信息失败',
                icon: 'none',
                mask: true,
                duration: 2000
              });
            }
          }
        })
      }      
    }
})
